package com.tarena.seata.cart.web.rpc;

import com.tarena.seata.api.CartApi;
import com.tarena.seata.cart.web.mapper.CartMapper;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import java.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CartApiImpl implements CartApi {
    @Autowired
    private CartMapper cartMapper;
    @GlobalTransactional
    @Override public void delCart(String userId, String productCode) {
        cartMapper.delCart(userId, productCode);
        System.out.println("seata全局事务id=============="+ RootContext.getXID());

        System.out.println("删除购物车失败,其他分支回滚");
        if (new Random().nextBoolean()){
            throw new RuntimeException("删除购物车失败");
        }


    }
}
